fix specs and add support for translation in Scenario exports

Andrew Cantino 9 gadi atpakaļ
vecāks
revīzija
b31a0ec3e7

+ 22 - 3
app/models/scenario_import.rb

@@ -142,7 +142,7 @@ class ScenarioImport
142 142
   def generate_diff
143 143
     @agent_diffs = (parsed_data['agents'] || []).map.with_index do |agent_data, index|
144 144
       # AgentDiff is defined at the end of this file.
145
-      agent_diff = AgentDiff.new(agent_data)
145
+      agent_diff = AgentDiff.new(agent_data, parsed_data['schema_version'])
146 146
       if existing_scenario
147 147
         # If this Agent exists already, update the AgentDiff with the local version's information.
148 148
         agent_diff.diff_with! existing_scenario.agents.find_by(:guid => agent_data['guid'])
@@ -184,14 +184,16 @@ class ScenarioImport
184 184
       end
185 185
     end
186 186
 
187
-    def initialize(agent_data)
187
+    def initialize(agent_data, schema_version)
188 188
       super()
189
+      @schema_version = schema_version
189 190
       @requires_merge = false
190 191
       self.agent = nil
191 192
       store! agent_data
192 193
     end
193 194
 
194 195
     BASE_FIELDS = %w[name schedule keep_events_for propagate_immediately disabled guid]
196
+    FIELDS_REQUIRING_TRANSLATION = %w[keep_events_for]
195 197
 
196 198
     def agent_exists?
197 199
       !!agent
@@ -209,10 +211,27 @@ class ScenarioImport
209 211
       self.type = FieldDiff.new(agent_data["type"].split("::").pop)
210 212
       self.options = FieldDiff.new(agent_data['options'] || {})
211 213
       BASE_FIELDS.each do |option|
212
-        self[option] = FieldDiff.new(agent_data[option]) if agent_data.has_key?(option)
214
+        if agent_data.has_key?(option)
215
+          value = agent_data[option]
216
+          value = send(:"translate_#{option}", value) if option.in?(FIELDS_REQUIRING_TRANSLATION)
217
+          self[option] = FieldDiff.new(value)
218
+        end
219
+      end
220
+    end
221
+
222
+    def translate_keep_events_for(old_value)
223
+      if schema_version < 1
224
+        # Was stored in days, now is stored in seconds.
225
+        old_value.to_i.days
226
+      else
227
+        old_value
213 228
       end
214 229
     end
215 230
 
231
+    def schema_version
232
+      (@schema_version || 0).to_i
233
+    end
234
+
216 235
     def diff_with!(agent)
217 236
       return unless agent.present?
218 237
 

+ 90 - 52
db/schema.rb

@@ -14,36 +14,36 @@
14 14
 ActiveRecord::Schema.define(version: 20150507153436) do
15 15
 
16 16
   create_table "agent_logs", force: :cascade do |t|
17
-    t.integer  "agent_id",          limit: 4,                 null: false
18
-    t.text     "message",           limit: 65535,             null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
19
-    t.integer  "level",             limit: 4,     default: 3, null: false
17
+    t.integer  "agent_id",          limit: 4,                    null: false
18
+    t.text     "message",           limit: 16777215,             null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
19
+    t.integer  "level",             limit: 4,        default: 3, null: false
20 20
     t.integer  "inbound_event_id",  limit: 4
21 21
     t.integer  "outbound_event_id", limit: 4
22
-    t.datetime "created_at"
23
-    t.datetime "updated_at"
22
+    t.datetime "created_at",                                     null: false
23
+    t.datetime "updated_at",                                     null: false
24 24
   end
25 25
 
26 26
   create_table "agents", force: :cascade do |t|
27 27
     t.integer  "user_id",               limit: 4
28
-    t.text     "options",               limit: 65535,                                   charset: "utf8mb4", collation: "utf8mb4_bin"
29
-    t.string   "type",                  limit: 191,                                                         collation: "utf8_bin"
30
-    t.string   "name",                  limit: 191,                                     charset: "utf8mb4", collation: "utf8mb4_bin"
31
-    t.string   "schedule",              limit: 191,                                                         collation: "utf8_bin"
28
+    t.text     "options",               limit: 16777215,                                charset: "utf8mb4", collation: "utf8mb4_bin"
29
+    t.string   "type",                  limit: 255,                                                         collation: "utf8_bin"
30
+    t.string   "name",                  limit: 255,                                     charset: "utf8mb4", collation: "utf8mb4_bin"
31
+    t.string   "schedule",              limit: 255,                                                         collation: "utf8_bin"
32 32
     t.integer  "events_count",          limit: 4,          default: 0,     null: false
33 33
     t.datetime "last_check_at"
34 34
     t.datetime "last_receive_at"
35 35
     t.integer  "last_checked_event_id", limit: 4
36
-    t.datetime "created_at"
37
-    t.datetime "updated_at"
36
+    t.datetime "created_at",                                               null: false
37
+    t.datetime "updated_at",                                               null: false
38 38
     t.text     "memory",                limit: 4294967295,                              charset: "utf8mb4", collation: "utf8mb4_bin"
39 39
     t.datetime "last_web_request_at"
40
-    t.integer  "keep_events_for",       limit: 4,          default: 0,     null: false
41 40
     t.datetime "last_event_at"
42 41
     t.datetime "last_error_log_at"
42
+    t.integer  "keep_events_for",       limit: 4,          default: 0,     null: false
43 43
     t.boolean  "propagate_immediately", limit: 1,          default: false, null: false
44 44
     t.boolean  "disabled",              limit: 1,          default: false, null: false
45
+    t.string   "guid",                  limit: 255,                        null: false, charset: "ascii",   collation: "ascii_bin"
45 46
     t.integer  "service_id",            limit: 4
46
-    t.string   "guid",                  limit: 191,                        null: false
47 47
   end
48 48
 
49 49
   add_index "agents", ["guid"], name: "index_agents_on_guid", using: :btree
@@ -51,6 +51,14 @@ ActiveRecord::Schema.define(version: 20150507153436) do
51 51
   add_index "agents", ["type"], name: "index_agents_on_type", using: :btree
52 52
   add_index "agents", ["user_id", "created_at"], name: "index_agents_on_user_id_and_created_at", using: :btree
53 53
 
54
+  create_table "contacts", force: :cascade do |t|
55
+    t.text     "message",    limit: 65535,              charset: "utf8mb4", collation: "utf8mb4_bin"
56
+    t.string   "name",       limit: 255,                charset: "utf8mb4", collation: "utf8mb4_bin"
57
+    t.string   "email",      limit: 255,                                    collation: "utf8_bin"
58
+    t.datetime "created_at",               null: false
59
+    t.datetime "updated_at",               null: false
60
+  end
61
+
54 62
   create_table "control_links", force: :cascade do |t|
55 63
     t.integer  "controller_id",     limit: 4, null: false
56 64
     t.integer  "control_target_id", limit: 4, null: false
@@ -64,15 +72,15 @@ ActiveRecord::Schema.define(version: 20150507153436) do
64 72
   create_table "delayed_jobs", force: :cascade do |t|
65 73
     t.integer  "priority",   limit: 4,        default: 0
66 74
     t.integer  "attempts",   limit: 4,        default: 0
67
-    t.text     "handler",    limit: 16777215,             charset: "utf8mb4", collation: "utf8mb4_bin"
68
-    t.text     "last_error", limit: 65535,                charset: "utf8mb4", collation: "utf8mb4_bin"
75
+    t.text     "handler",    limit: 16777215,                          charset: "utf8mb4", collation: "utf8mb4_bin"
76
+    t.text     "last_error", limit: 16777215,                          charset: "utf8mb4", collation: "utf8mb4_bin"
69 77
     t.datetime "run_at"
70 78
     t.datetime "locked_at"
71 79
     t.datetime "failed_at"
72
-    t.string   "locked_by",  limit: 191
73
-    t.string   "queue",      limit: 191
74
-    t.datetime "created_at"
75
-    t.datetime "updated_at"
80
+    t.string   "locked_by",  limit: 255
81
+    t.string   "queue",      limit: 255
82
+    t.datetime "created_at",                              null: false
83
+    t.datetime "updated_at",                              null: false
76 84
   end
77 85
 
78 86
   add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
@@ -80,11 +88,11 @@ ActiveRecord::Schema.define(version: 20150507153436) do
80 88
   create_table "events", force: :cascade do |t|
81 89
     t.integer  "user_id",    limit: 4
82 90
     t.integer  "agent_id",   limit: 4
83
-    t.decimal  "lat",                         precision: 15, scale: 10
84
-    t.decimal  "lng",                         precision: 15, scale: 10
85
-    t.text     "payload",    limit: 16777215,                           charset: "utf8mb4", collation: "utf8mb4_bin"
86
-    t.datetime "created_at"
87
-    t.datetime "updated_at"
91
+    t.decimal  "lat",                           precision: 15, scale: 10
92
+    t.decimal  "lng",                           precision: 15, scale: 10
93
+    t.text     "payload",    limit: 4294967295,                                        charset: "utf8mb4", collation: "utf8mb4_bin"
94
+    t.datetime "created_at",                                              null: false
95
+    t.datetime "updated_at",                                              null: false
88 96
     t.datetime "expires_at"
89 97
   end
90 98
 
@@ -95,14 +103,27 @@ ActiveRecord::Schema.define(version: 20150507153436) do
95 103
   create_table "links", force: :cascade do |t|
96 104
     t.integer  "source_id",            limit: 4
97 105
     t.integer  "receiver_id",          limit: 4
98
-    t.datetime "created_at"
99
-    t.datetime "updated_at"
106
+    t.datetime "created_at",                                 null: false
107
+    t.datetime "updated_at",                                 null: false
100 108
     t.integer  "event_id_at_creation", limit: 4, default: 0, null: false
101 109
   end
102 110
 
103 111
   add_index "links", ["receiver_id", "source_id"], name: "index_links_on_receiver_id_and_source_id", using: :btree
104 112
   add_index "links", ["source_id", "receiver_id"], name: "index_links_on_source_id_and_receiver_id", using: :btree
105 113
 
114
+  create_table "rails_admin_histories", force: :cascade do |t|
115
+    t.text     "message",    limit: 65535,              charset: "latin1", collation: "latin1_swedish_ci"
116
+    t.string   "username",   limit: 255,                charset: "latin1", collation: "latin1_swedish_ci"
117
+    t.integer  "item",       limit: 4
118
+    t.string   "table",      limit: 255,                charset: "latin1", collation: "latin1_swedish_ci"
119
+    t.integer  "month",      limit: 2
120
+    t.integer  "year",       limit: 8
121
+    t.datetime "created_at",               null: false
122
+    t.datetime "updated_at",               null: false
123
+  end
124
+
125
+  add_index "rails_admin_histories", ["item", "table", "month", "year"], name: "index_rails_admin_histories", using: :btree
126
+
106 127
   create_table "scenario_memberships", force: :cascade do |t|
107 128
     t.integer  "agent_id",    limit: 4, null: false
108 129
     t.integer  "scenario_id", limit: 4, null: false
@@ -114,69 +135,86 @@ ActiveRecord::Schema.define(version: 20150507153436) do
114 135
   add_index "scenario_memberships", ["scenario_id"], name: "index_scenario_memberships_on_scenario_id", using: :btree
115 136
 
116 137
   create_table "scenarios", force: :cascade do |t|
117
-    t.string   "name",         limit: 191,                   null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
138
+    t.string   "name",         limit: 255,                   null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
118 139
     t.integer  "user_id",      limit: 4,                     null: false
119 140
     t.datetime "created_at"
120 141
     t.datetime "updated_at"
121 142
     t.text     "description",  limit: 65535,                              charset: "utf8mb4", collation: "utf8mb4_bin"
122 143
     t.boolean  "public",       limit: 1,     default: false, null: false
123
-    t.string   "guid",         limit: 191,                   null: false, charset: "ascii",   collation: "ascii_bin"
124
-    t.string   "source_url",   limit: 191
125
-    t.string   "tag_bg_color", limit: 191
126
-    t.string   "tag_fg_color", limit: 191
144
+    t.string   "guid",         limit: 255,                   null: false, charset: "ascii",   collation: "ascii_bin"
145
+    t.string   "source_url",   limit: 255
146
+    t.string   "tag_bg_color", limit: 255
147
+    t.string   "tag_fg_color", limit: 255
127 148
   end
128 149
 
129 150
   add_index "scenarios", ["user_id", "guid"], name: "index_scenarios_on_user_id_and_guid", unique: true, using: :btree
130 151
 
131 152
   create_table "services", force: :cascade do |t|
132 153
     t.integer  "user_id",       limit: 4,                     null: false
133
-    t.string   "provider",      limit: 191,                   null: false
134
-    t.string   "name",          limit: 191,                   null: false
135
-    t.text     "token",         limit: 65535,                 null: false
136
-    t.text     "secret",        limit: 65535
137
-    t.text     "refresh_token", limit: 65535
154
+    t.string   "provider",      limit: 255,                   null: false, charset: "latin1", collation: "latin1_swedish_ci"
155
+    t.string   "name",          limit: 255,                   null: false, charset: "latin1", collation: "latin1_swedish_ci"
156
+    t.text     "token",         limit: 65535,                 null: false, charset: "latin1", collation: "latin1_swedish_ci"
157
+    t.text     "secret",        limit: 65535,                              charset: "latin1", collation: "latin1_swedish_ci"
158
+    t.text     "refresh_token", limit: 65535,                              charset: "latin1", collation: "latin1_swedish_ci"
138 159
     t.datetime "expires_at"
139 160
     t.boolean  "global",        limit: 1,     default: false
140
-    t.text     "options",       limit: 65535
161
+    t.text     "options",       limit: 65535,                              charset: "latin1", collation: "latin1_swedish_ci"
141 162
     t.datetime "created_at"
142 163
     t.datetime "updated_at"
143
-    t.string   "uid",           limit: 191
164
+    t.string   "uid",           limit: 255,                                charset: "latin1", collation: "latin1_swedish_ci"
144 165
   end
145 166
 
146 167
   add_index "services", ["provider"], name: "index_services_on_provider", using: :btree
147 168
   add_index "services", ["uid"], name: "index_services_on_uid", using: :btree
148 169
   add_index "services", ["user_id", "global"], name: "index_services_on_user_id_and_global", using: :btree
149 170
 
171
+  create_table "taggings", force: :cascade do |t|
172
+    t.integer  "tag_id",        limit: 4
173
+    t.integer  "taggable_id",   limit: 4
174
+    t.string   "taggable_type", limit: 255, collation: "utf8_general_ci"
175
+    t.integer  "tagger_id",     limit: 4
176
+    t.string   "tagger_type",   limit: 255, collation: "utf8_general_ci"
177
+    t.string   "context",       limit: 128, collation: "utf8_general_ci"
178
+    t.datetime "created_at"
179
+  end
180
+
181
+  add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
182
+  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
183
+
184
+  create_table "tags", force: :cascade do |t|
185
+    t.string "name", limit: 255, collation: "utf8_general_ci"
186
+  end
187
+
150 188
   create_table "user_credentials", force: :cascade do |t|
151 189
     t.integer  "user_id",          limit: 4,                      null: false
152
-    t.string   "credential_name",  limit: 191,                    null: false
190
+    t.string   "credential_name",  limit: 255,                    null: false
153 191
     t.text     "credential_value", limit: 65535,                  null: false
154
-    t.datetime "created_at"
155
-    t.datetime "updated_at"
156
-    t.string   "mode",             limit: 191,   default: "text", null: false, collation: "utf8_bin"
192
+    t.datetime "created_at",                                      null: false
193
+    t.datetime "updated_at",                                      null: false
194
+    t.string   "mode",             limit: 255,   default: "text", null: false, collation: "utf8_bin"
157 195
   end
158 196
 
159 197
   add_index "user_credentials", ["user_id", "credential_name"], name: "index_user_credentials_on_user_id_and_credential_name", unique: true, using: :btree
160 198
 
161 199
   create_table "users", force: :cascade do |t|
162
-    t.string   "email",                  limit: 191, default: "",    null: false,                     collation: "utf8_bin"
163
-    t.string   "encrypted_password",     limit: 191, default: "",    null: false, charset: "ascii",   collation: "ascii_bin"
164
-    t.string   "reset_password_token",   limit: 191,                                                  collation: "utf8_bin"
200
+    t.string   "email",                  limit: 255, default: "",    null: false,                     collation: "utf8_bin"
201
+    t.string   "encrypted_password",     limit: 255, default: "",    null: false, charset: "ascii",   collation: "ascii_bin"
202
+    t.string   "reset_password_token",   limit: 255,                                                  collation: "utf8_bin"
165 203
     t.datetime "reset_password_sent_at"
166 204
     t.datetime "remember_created_at"
167 205
     t.integer  "sign_in_count",          limit: 4,   default: 0
168 206
     t.datetime "current_sign_in_at"
169 207
     t.datetime "last_sign_in_at"
170
-    t.string   "current_sign_in_ip",     limit: 191
171
-    t.string   "last_sign_in_ip",        limit: 191
172
-    t.datetime "created_at"
173
-    t.datetime "updated_at"
208
+    t.string   "current_sign_in_ip",     limit: 255
209
+    t.string   "last_sign_in_ip",        limit: 255
210
+    t.datetime "created_at",                                         null: false
211
+    t.datetime "updated_at",                                         null: false
174 212
     t.boolean  "admin",                  limit: 1,   default: false, null: false
175 213
     t.integer  "failed_attempts",        limit: 4,   default: 0
176
-    t.string   "unlock_token",           limit: 191
214
+    t.string   "unlock_token",           limit: 255
177 215
     t.datetime "locked_at"
178
-    t.string   "username",               limit: 191,                              charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
179
-    t.string   "invitation_code",        limit: 191,                 null: false
216
+    t.string   "username",               limit: 191,                 null: false, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
217
+    t.string   "invitation_code",        limit: 255,                 null: false,                     collation: "utf8_bin"
180 218
     t.integer  "scenario_count",         limit: 4,   default: 0,     null: false
181 219
   end
182 220
 

+ 1 - 0
lib/agents_exporter.rb

@@ -12,6 +12,7 @@ class AgentsExporter
12 12
 
13 13
   def as_json(opts = {})
14 14
     {
15
+      :schema_version => 1,
15 16
       :name => options[:name].presence || 'No name provided',
16 17
       :description => options[:description].presence || 'No description provided',
17 18
       :source_url => options[:source_url],

+ 2 - 2
spec/fixtures/agents.yml

@@ -38,7 +38,7 @@ bob_weather_agent:
38 38
   schedule: "midnight"
39 39
   name: "SF Weather"
40 40
   guid: <%= SecureRandom.hex %>
41
-  keep_events_for: 45
41
+  keep_events_for: <%= 45.days %>
42 42
   options: <%= { :location => 94102, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
43 43
 
44 44
 jane_weather_agent:
@@ -47,7 +47,7 @@ jane_weather_agent:
47 47
   schedule: "midnight"
48 48
   name: "SF Weather"
49 49
   guid: <%= SecureRandom.hex %>
50
-  keep_events_for: 30
50
+  keep_events_for: <%= 30.days %>
51 51
   options: <%= { :location => 94103, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
52 52
 
53 53
 jane_rain_notifier_agent:

+ 1 - 0
spec/lib/agents_exporter_spec.rb

@@ -21,6 +21,7 @@ describe AgentsExporter do
21 21
       expect(data[:description]).to eq(description)
22 22
       expect(data[:source_url]).to eq(source_url)
23 23
       expect(data[:guid]).to eq(guid)
24
+      expect(data[:schema_version]).to eq(1)
24 25
       expect(data[:tag_fg_color]).to eq(tag_fg_color)
25 26
       expect(data[:tag_bg_color]).to eq(tag_bg_color)
26 27
       expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)

+ 11 - 11
spec/models/agent_spec.rb

@@ -546,11 +546,11 @@ describe Agent do
546 546
         expect(agent).to have(1).errors_on(:keep_events_for)
547 547
         agent.keep_events_for = ""
548 548
         expect(agent).to have(1).errors_on(:keep_events_for)
549
-        agent.keep_events_for = 5
549
+        agent.keep_events_for = 5.days.to_i
550 550
         expect(agent).to be_valid
551 551
         agent.keep_events_for = 0
552 552
         expect(agent).to be_valid
553
-        agent.keep_events_for = 365
553
+        agent.keep_events_for = 365.days.to_i
554 554
         expect(agent).to be_valid
555 555
 
556 556
         # Rails seems to call to_i on the input. This guards against future changes to that behavior.
@@ -564,7 +564,7 @@ describe Agent do
564 564
         @time = "2014-01-01 01:00:00 +00:00"
565 565
         time_travel_to @time do
566 566
           @agent = Agents::SomethingSource.new(:name => "something")
567
-          @agent.keep_events_for = 5
567
+          @agent.keep_events_for = 5.days
568 568
           @agent.user = users(:bob)
569 569
           @agent.save!
570 570
           @event = @agent.create_event :payload => { "hello" => "world" }
@@ -580,7 +580,7 @@ describe Agent do
580 580
           @agent.save!
581 581
 
582 582
           @agent.options[:foo] = "bar1"
583
-          @agent.keep_events_for = 5
583
+          @agent.keep_events_for = 5.days
584 584
           @agent.save!
585 585
         end
586 586
       end
@@ -590,7 +590,7 @@ describe Agent do
590 590
           time_travel_to @time do
591 591
             expect {
592 592
                 @agent.options[:foo] = "bar1"
593
-                @agent.keep_events_for = 3
593
+                @agent.keep_events_for = 3.days
594 594
                 @agent.save!
595 595
             }.to change { @event.reload.expires_at }
596 596
             expect(@event.expires_at.to_i).to be_within(2).of(3.days.from_now.to_i)
@@ -603,7 +603,7 @@ describe Agent do
603 603
 
604 604
           expect {
605 605
             @agent.options[:foo] = "bar2"
606
-            @agent.keep_events_for = 3
606
+            @agent.keep_events_for = 3.days
607 607
             @agent.save!
608 608
           }.to change { @event.reload.expires_at }
609 609
           expect(@event.expires_at.to_i).to be_within(60 * 61).of(1.days.from_now.to_i) # The larger time is to deal with daylight savings
@@ -635,7 +635,7 @@ describe Agent do
635 635
         @receiver = Agents::CannotBeScheduled.new(
636 636
           name: 'Agent',
637 637
           options: { foo: 'bar3' },
638
-          keep_events_for: 3,
638
+          keep_events_for: 3.days,
639 639
           propagate_immediately: true)
640 640
         @receiver.user = users(:bob)
641 641
         @receiver.sources << @sender
@@ -747,7 +747,7 @@ describe Agent do
747 747
 
748 748
       it "sets expires_at on created events" do
749 749
         event = agents(:jane_weather_agent).create_event :payload => { 'hi' => 'there' }
750
-        expect(event.expires_at.to_i).to be_within(5).of(agents(:jane_weather_agent).keep_events_for.days.from_now.to_i)
750
+        expect(event.expires_at.to_i).to be_within(5).of(agents(:jane_weather_agent).keep_events_for.seconds.from_now.to_i)
751 751
       end
752 752
     end
753 753
 
@@ -836,7 +836,7 @@ describe AgentDrop do
836 836
         },
837 837
       },
838 838
       schedule: 'every_1h',
839
-      keep_events_for: 2)
839
+      keep_events_for: 2.days)
840 840
     @wsa1.user = users(:bob)
841 841
     @wsa1.save!
842 842
 
@@ -853,7 +853,7 @@ describe AgentDrop do
853 853
         },
854 854
       },
855 855
       schedule: 'every_12h',
856
-      keep_events_for: 2)
856
+      keep_events_for: 2.days)
857 857
     @wsa2.user = users(:bob)
858 858
     @wsa2.save!
859 859
 
@@ -868,7 +868,7 @@ describe AgentDrop do
868 868
         matchers: [],
869 869
         skip_created_at: 'false',
870 870
       },
871
-      keep_events_for: 2,
871
+      keep_events_for: 2.days,
872 872
       propagate_immediately: true)
873 873
     @efa.user = users(:bob)
874 874
     @efa.sources << @wsa1 << @wsa2

+ 55 - 8
spec/models/scenario_import_spec.rb

@@ -30,7 +30,7 @@ describe ScenarioImport do
30 30
       :type => "Agents::WeatherAgent",
31 31
       :name => "a weather agent",
32 32
       :schedule => "5pm",
33
-      :keep_events_for => 14,
33
+      :keep_events_for => 14.days,
34 34
       :disabled => true,
35 35
       :guid => "a-weather-agent",
36 36
       :options => weather_agent_options
@@ -61,6 +61,7 @@ describe ScenarioImport do
61 61
   end
62 62
   let(:valid_parsed_data) do
63 63
     {
64
+      :schema_version => 1,
64 65
       :name => name,
65 66
       :description => description,
66 67
       :guid => guid,
@@ -203,7 +204,7 @@ describe ScenarioImport do
203 204
 
204 205
           expect(weather_agent.name).to eq("a weather agent")
205 206
           expect(weather_agent.schedule).to eq("5pm")
206
-          expect(weather_agent.keep_events_for).to eq(14)
207
+          expect(weather_agent.keep_events_for).to eq(14.days)
207 208
           expect(weather_agent.propagate_immediately).to be_falsey
208 209
           expect(weather_agent).to be_disabled
209 210
           expect(weather_agent.memory).to be_empty
@@ -226,6 +227,23 @@ describe ScenarioImport do
226 227
             scenario_import.import
227 228
           }.to change { users(:bob).agents.count }.by(2)
228 229
         end
230
+
231
+        context "when the schema_version is less than 1" do
232
+          before do
233
+            valid_parsed_weather_agent_data[:keep_events_for] = 2
234
+            valid_parsed_data.delete(:schema_version)
235
+          end
236
+
237
+          it "translates keep_events_for from days to seconds" do
238
+            scenario_import.import
239
+            expect(scenario_import.errors).to be_empty
240
+            weather_agent = scenario_import.scenario.agents.find_by(:guid => "a-weather-agent")
241
+            trigger_agent = scenario_import.scenario.agents.find_by(:guid => "a-trigger-agent")
242
+
243
+            expect(weather_agent.keep_events_for).to eq(2.days)
244
+            expect(trigger_agent.keep_events_for).to eq(0)
245
+          end
246
+        end
229 247
       end
230 248
 
231 249
       describe "#generate_diff" do
@@ -309,7 +327,7 @@ describe ScenarioImport do
309 327
 
310 328
           expect(weather_agent.name).to eq("a weather agent")
311 329
           expect(weather_agent.schedule).to eq("5pm")
312
-          expect(weather_agent.keep_events_for).to eq(14)
330
+          expect(weather_agent.keep_events_for).to eq(14.days)
313 331
           expect(weather_agent.propagate_immediately).to be_falsey
314 332
           expect(weather_agent).to be_disabled
315 333
           expect(weather_agent.memory).to be_empty
@@ -330,7 +348,7 @@ describe ScenarioImport do
330 348
             "0" => {
331 349
               "name" => "updated name",
332 350
               "schedule" => "6pm",
333
-              "keep_events_for" => "2",
351
+              "keep_events_for" => 2.days.to_i,
334 352
               "disabled" => "false",
335 353
               "options" => weather_agent_options.merge("api_key" => "foo").to_json
336 354
             }
@@ -343,7 +361,7 @@ describe ScenarioImport do
343 361
           weather_agent = existing_scenario.agents.find_by(:guid => "a-weather-agent")
344 362
           expect(weather_agent.name).to eq("updated name")
345 363
           expect(weather_agent.schedule).to eq("6pm")
346
-          expect(weather_agent.keep_events_for).to eq(2)
364
+          expect(weather_agent.keep_events_for).to eq(2.days)
347 365
           expect(weather_agent).not_to be_disabled
348 366
           expect(weather_agent.options).to eq(weather_agent_options.merge("api_key" => "foo"))
349 367
         end
@@ -353,7 +371,7 @@ describe ScenarioImport do
353 371
             "0" => {
354 372
               "name" => "",
355 373
               "schedule" => "foo",
356
-              "keep_events_for" => "2",
374
+              "keep_events_for" => 2.days.to_i.to_s,
357 375
               "options" => weather_agent_options.merge("api_key" => "").to_json
358 376
             }
359 377
           }
@@ -386,12 +404,40 @@ describe ScenarioImport do
386 404
           end
387 405
         end
388 406
 
407
+        context "when the schema_version is less than 1" do
408
+          it "translates keep_events_for from days to seconds" do
409
+            valid_parsed_data.delete(:schema_version)
410
+            valid_parsed_data[:agents] = [valid_parsed_weather_agent_data.merge(keep_events_for: 5)]
411
+
412
+            scenario_import.merges = {
413
+              "0" => {
414
+                "name" => "a new name",
415
+                "schedule" => "6pm",
416
+                "keep_events_for" => 2.days.to_i.to_s,
417
+                "disabled" => "true",
418
+                "options" => weather_agent_options.merge("api_key" => "foo").to_json
419
+              }
420
+            }
421
+
422
+            expect(scenario_import).to be_valid
423
+
424
+            weather_agent_diff = scenario_import.agent_diffs[0]
425
+
426
+            expect(weather_agent_diff.name.current).to eq(agents(:bob_weather_agent).name)
427
+            expect(weather_agent_diff.name.incoming).to eq('a weather agent')
428
+            expect(weather_agent_diff.name.updated).to eq('a new name')
429
+            expect(weather_agent_diff.keep_events_for.current).to eq(45.days.to_i)
430
+            expect(weather_agent_diff.keep_events_for.incoming).to eq(5.days.to_i)
431
+            expect(weather_agent_diff.keep_events_for.updated).to eq(2.days.to_i.to_s)
432
+          end
433
+        end
434
+
389 435
         it "sets the 'updated' FieldDiff values based on any feedback from the user" do
390 436
           scenario_import.merges = {
391 437
             "0" => {
392 438
               "name" => "a new name",
393 439
               "schedule" => "6pm",
394
-              "keep_events_for" => "2",
440
+              "keep_events_for" => 2.days.to_s,
395 441
               "disabled" => "true",
396 442
               "options" => weather_agent_options.merge("api_key" => "foo").to_json
397 443
             },
@@ -411,7 +457,8 @@ describe ScenarioImport do
411 457
           expect(weather_agent_diff.name.updated).to eq("a new name")
412 458
 
413 459
           expect(weather_agent_diff.schedule.updated).to eq("6pm")
414
-          expect(weather_agent_diff.keep_events_for.updated).to eq("2")
460
+          expect(weather_agent_diff.keep_events_for.current).to eq(45.days)
461
+          expect(weather_agent_diff.keep_events_for.updated).to eq(2.days.to_s)
415 462
           expect(weather_agent_diff.disabled.updated).to eq("true")
416 463
           expect(weather_agent_diff.options.updated).to eq(weather_agent_options.merge("api_key" => "foo"))
417 464
         end